#include "BLDC_change.h"
uint32_t modulation_value = 0;
char adc_check_flag = 0, speed_check_flag = 0;
pole_status pole_status_set;
u16 i_adc_max_average = 0, i_adc_min_average = 0;
uint32_t period_add_sub_value = 0, period_add_sub_value_3_in_4 = 0, PWM_PERIOD_half = 0;
uint16_t block_time = 14, rise_voltage_block = 30, down_voltage_block = 0;
char speed_real;
extern u32 duty_limit_setting;
extern uint16_t u16ADC1[10];
extern uint32_t u16ADC1_16in1_adc[10];
extern uint32_t u16ADC1_2in1_adc[10];

void clear_max_flag(void)
{
    i_adc_max_average = 0;
}
void clear_min_flag(void)
{
    i_adc_min_average = 0;
}
//调制度输入
void BLDC_motor_modulation_value_set(uint32_t modulation)
{
    compare_init();
    if (modulation < duty_limit_setting)
    {
        modulation_value = modulation;
        period_add_sub_value = PWM_PERIOD * modulation_value / 1000;
        period_add_sub_value_3_in_4 = period_add_sub_value - (period_add_sub_value >> 2);
    }
}
uint32_t get_modulation_value(void)
{
    return modulation_value;
}
//手动设定
void motor_status_set(char u_status, char v_status, char w_status)
{
    pole_status_set.u_pole_status = u_status;
    pole_status_set.v_pole_status = v_status;
    pole_status_set.w_pole_status = w_status;
}
//设定换向函数
void motor_status_change_auto(void)
{
    pole_status_set.u_pole_status++;
    pole_status_set.v_pole_status++;
    pole_status_set.w_pole_status++;
    if (pole_status_set.u_pole_status >= 6)
    {
        pole_status_set.u_pole_status = 0;
    }
    if (pole_status_set.v_pole_status >= 6)
    {
        pole_status_set.v_pole_status = 0;
    }
    if (pole_status_set.w_pole_status >= 6)
    {
        pole_status_set.w_pole_status = 0;
    }
}
//设定比较值
void compare_init(void)
{
    PWM_PERIOD_half = PWM_PERIOD / 2;
}
uint32_t get_mid_compare_now(char pole_status)
{
    switch (pole_status)
    {
    case 0:
        return PWM_PERIOD_half - period_add_sub_value;
    case 1:
        return PWM_PERIOD_half - period_add_sub_value_3_in_4;
    case 3:
    case 4:
        return PWM_PERIOD_half + period_add_sub_value;
    }
    return PWM_PERIOD_half; // case 2、5
}
//中间换向
void BLDC_mid_change_mode_compare(void)
{
    TIM1->CCR1 = get_compare_now(pole_status_set.u_pole_status);
    TIM1->CCR2 = get_compare_now(pole_status_set.v_pole_status);
    TIM1->CCR3 = get_compare_now(pole_status_set.w_pole_status);
}
//计算换向幅度
uint32_t get_compare_now(char pole_status)
{
    //    uint32_t return_number = 0;
    switch (pole_status)
    {
    case 0:
        return PWM_PERIOD_half - period_add_sub_value_3_in_4;
    case 1:
        return PWM_PERIOD_half - period_add_sub_value;
    case 3:
    case 4:
        return PWM_PERIOD_half + period_add_sub_value;
    }
    return PWM_PERIOD_half; // case 2、5
}

//计算比较值
void BLDC_change_mode_compare(void)
{
    TIM1->CCR1 = get_compare_now(pole_status_set.u_pole_status);
    TIM1->CCR2 = get_compare_now(pole_status_set.v_pole_status);
    TIM1->CCR3 = get_compare_now(pole_status_set.w_pole_status);
    // change_on_off_status();
}
//电机直接换向调用
void motor_status_change_check_and_set()
{
    if (adc_check_flag && speed_check_flag)
    {
        adc_check_flag = 0;
        speed_check_flag = 0;
        // protect_counter = 0;
        motor_status_change_auto();
        BLDC_change_mode_compare();
        speed_real++; //
    }
}
void motor_status_change_with_out_check(void)
{
    adc_check_flag = 0;
    speed_check_flag = 0;
    // protect_counter = 0;
    motor_status_change_auto();
    BLDC_change_mode_compare();
    speed_real++;
}
//设定比较值
void adc_flag_set_and_check_status(void)
{
    adc_check_flag = 1;
}
void speed_flag_set_and_check_status(void)
{
    speed_check_flag = 1;
}
void change_on_off_status(void)
{
    switch (pole_status_set.u_pole_status)
    {
    case 0:
    case 1:
    case 3:
    case 4:
        UL_PIN_IO_MODE_OUTPUT_HIGH;
        break;
    case 2:
    case 5:
        UL_PIN_IO_MODE_OUTPUT_LOW;
        break;
    }
    switch (pole_status_set.v_pole_status)
    {
    case 0:
    case 1:
    case 3:
    case 4:
        VL_PIN_IO_MODE_OUTPUT_HIGH;
        break;
    case 2:
    case 5:
        VL_PIN_IO_MODE_OUTPUT_LOW;
        break;
    }
    switch (pole_status_set.w_pole_status)
    {
    case 0:
    case 1:
    case 3:
    case 4:
        WL_PIN_IO_MODE_OUTPUT_HIGH;
        break;
    case 2:
    case 5:
        WL_PIN_IO_MODE_OUTPUT_LOW;
        break;
    }
}
